﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SpringWaterDataAccess;
using FredCK.FCKeditorV2;

namespace client
{
    /// <summary>
    /// Summary description for bloggerinput.
    /// </summary>
    public partial class bloggerinput : System.Web.UI.Page
    {

        protected int userID = 0;
        protected string postID = "";
                
        protected void Page_Load(object sender, System.EventArgs e)
        {
            userID = Convert.ToUInt16(Request.QueryString["UserID"]);
            postID = Request.QueryString["PostID"];

            //only allow file upload if it's checked by user
            File1.Disabled = !CheckBox1.Checked || (postID != null && postID != "");
			//FCKeditor1.ToolbarSet = "Basic";
           


            // Put user code to initialize the page here
            if (!IsPostBack) 
            {
                ForumDataAccess da = new ForumDataAccess();

                int uid = Convert.ToUInt16(Request.QueryString["UserID"]);
                int total = 0;
                da.getPosts(uid, 0,0, 0,0, 0, out total, "blog", "", 0, "CreateDate");
                // Check if this user has any blog posts

                /*DataSet userDS  = da.GetUserByID(userID);
                string userType = Convert.ToString(userDS.Tables[0].Rows[0]["type"]);

                if (total == 0 && (userType == null || userType == "") ) 
                {
                    Response.Redirect("bloggerrule.aspx?UserID=" + userID);
                } */

                DataTable  dt = da.GetForumCategories(11).Tables[0];
                                
                DropDownList1.DataSource =dt;
                DropDownList1.DataTextField = "CategoryName";
                DropDownList1.DataValueField = "CategoryID";
                DropDownList1.SelectedValue = "44";

                DropDownList1.DataBind();
                    
                if (postID != null && postID != "") /* edit existing */ 
                {
                    DataSet ds = da.GetPostDetailByPostID(postID);
                    Text1.Value = Convert.ToString(ds.Tables[0].Rows[0]["Subject"]);
                    DropDownList1.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["CategoryID"]);
                    Dropdownlist2.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["Status"]);
                    FCKeditor1.Value = Convert.ToString(ds.Tables[0].Rows[0]["Body"]);
                    TEXTAREA1.Value = Convert.ToString(ds.Tables[0].Rows[0]["Summary"]);
                }
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    

		}
        #endregion

       protected void Button5_Click1(object sender, EventArgs e)
        {
            ForumDataAccess da = new ForumDataAccess();
            string forumID = "";
            int catID = Convert.ToUInt16(DropDownList1.SelectedValue);

			string status = Dropdownlist2.SelectedValue;

            if (postID == null || postID == "") 
            {                
                string fn = "";

                if (CheckBox1.Checked)
                {
                    if ((File1.PostedFile.FileName.Length > 0) &&
                        (File1.PostedFile.ContentLength > 0 ) &&
                        (File1.PostedFile.ContentLength < 64 * 1024 * 1024))
                    {
                        string ext = System.IO.Path.GetExtension(File1.PostedFile.FileName);

                        if (ext.ToLower().Equals(".doc") || ext.ToLower().Equals(".txt") || ext.ToLower().Equals(".mp3") ||
                            ext.ToLower().Equals(".mpg") || ext.ToLower().Equals(".wmv") || ext.ToLower().Equals(".jpg") ||
                            ext.ToLower().Equals(".bmp") ||
                            ext.ToLower().Equals(".ra")  || ext.ToLower().Equals(".rm"))
                        {
                            DateTime CurrTime = DateTime.Now;

                            fn = userID + "_" + CurrTime.Year + "-" + CurrTime.Month + "-" + CurrTime.Day +
                                CurrTime.DayOfWeek + CurrTime.Hour + "-" + CurrTime.Minute + "-"
                                + CurrTime.Second + "-" + CurrTime.Millisecond
                                + System.IO.Path.GetFileName(File1.PostedFile.FileName);

                            string SaveLocation = Server.MapPath("Data") + "\\" + fn;

                            try
                            {
                                File1.PostedFile.SaveAs(SaveLocation);
                            }
                            catch ( Exception ex )
                            {
                                Response.Write("Error: " + ex.Message);
                                return;
                                //Note: Exception.Message returns a detailed message that describes the current exception.
                                //For security reasons, we do not recommend that you return Exception.Message to end users in
                                //production environments. It would be better to put a generic error message.
                            }
                        }
                        else
                        {
                            Response.Write("Error: File with " + ext + " extension is not supported." );
                            return;
                        }
                    }
                    else
                    {
                        Response.Write("Please select a file less than 64MB to upload.");
                        return;
                    }
                }
                else if ( 0 == FCKeditor1.Value.Length )
                {
                    Response.Write("Empty content not posted.");
                    return;
                }

                da.InsertPostWithStatus(0, catID, status, fn, Text1.Value, FCKeditor1.Value, 
                    userID, TEXTAREA1.Value, "blog", out forumID);
            } 
            else 
            {
                da.EditPost(Convert.ToUInt16(postID),catID,Text1.Value, 
                    FCKeditor1.Value,TEXTAREA1.Value,status);
            }

            sow.CacheHelper cacheHelper = new sow.CacheHelper();
            cacheHelper.CleanAllCache();
            Response.Redirect("bloggerweb.aspx?UserID= " + userID + "&PostID=" + postID);
        }

        protected void Button2_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("bloggerinput.aspx?userID=" + userID);
        }

        protected void Button3_Click(object sender, System.EventArgs e)
        {
            
        }

       
        protected void Button4_Click(object sender, System.EventArgs e)
        {
            
        }

        

        protected void Button6_Click1(object sender, EventArgs e)
        {
            Button5_Click1(sender, e);
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            Response.Redirect("bloggerhome.aspx?userID=" + userID);
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {

        }

        protected void Button3_Click1(object sender, EventArgs e)
        {
            Response.Redirect("bloggerarticles.aspx?userID=" + userID);
        }

        protected void Button4_Click1(object sender, EventArgs e)
        {
            Response.Redirect("bloggercomments.aspx?userID=" + userID);
        }

       
     
    }
}
